(function () {

    var del=function(){
        $(document).on("click",".delete",function (e) {
            console.log(11);
            let gid=$(e.target).parent().siblings().eq(2).html();
            console.log(gid);
            if (JSON.parse(sessionStorage.getItem("userinfo"))) {
                let User = JSON.parse(sessionStorage.getItem("userinfo"));
                console.log(User);
                let obj = {
                    "uid": User.u_id,
                    "gid": gid
                }
                console.log(obj);
                $.ajax({
                    url: "../../server/delete.php",
                    data: obj,
                    type: "post"
                }).then(function (res) {
                    console.log(res);
                    $(e.target).parents("tr").remove();
                })
            } else {
                //未登录
                if ($.cookie("carts")) {
                    console.log(11);
                    let cookieList = JSON.parse($.cookie("carts"));
                    console.log(cookieList);
                    cookieList.some((ele, index) => {
                        if (ele.gid == gid) {
                            cookieList.splice(index, 1);
                            $.cookie("carts", JSON.stringify(cookieList));
                            $(e.target).parents("tr").remove();
                        }
                    })
                }
            }

        })
    }
    del();
    $(function () {
        if (sessionStorage.getItem("userinfo") == null) {
            //1.如果没登录,存入cookie
            //1.1点击之后,把cookie里的数据读取出来
            let cookieList = JSON.parse($.cookie("carts") || '[]');
            console.log(cookieList);
            if (cookieList.length>=1){
                let str=``;
                let total=0;
                let number1=0;
                let number2=0;
                let arr=[];
                cookieList.forEach((ele,index)=>{
                    str+=`<tr>
						<td><input type="checkbox" checked></td>
						<td><img src="${ele.img[0]}" alt=""><span>${ele.goodsName}</span></td>
						<td>${ele.gid}</td>
						<td>¥<span>${ele.price}</span>.00</td>
						<td>有库存</td>
						<td>是</td>
						<td data-id="${ele.gid}"><a href="javascript:;;" class="down">-</a><input type="text" value="${ele.num}" name="count"><a href="javascript:;;" class="up">+</a></td>
						<td>¥ <span>${ele.price*ele.num}</span>.00</td>
						<td><a href="">收藏</a><a href="javascript:;" class="delete">删除</a></td>
					  </tr>`
                    total+=ele.price*ele.num;
                    number2+=Number(ele.num);
                    arr.push(str);
                })
                $(".statistical table tr td:last-child span").html(total);
                $("#num4").html(total);
                $("#num1").html(arr.length);
                $("#num2").html(number2);
                $(".shoppingCar_tab_bottom table tbody").html(str);

                let gid=$(".up").parent().data("id");
                $(document).on("click",".up",function () {
                    let nun=Number($(this).parent().find("[name=count]").val())+1;
                    $(this).parent().find("[name=count]").val(nun);
                    updateCookie(nun,gid,$(this));
                })
                $(document).on("click",".down",function () {
                    let nun=Number($(this).parent().find("[name=count]").val())-1;
                    if (nun<1){
                        nun=1;
                    }
                    $(this).parent().find("[name=count]").val(nun);
                    //updateCount(oUser.u_id,gid,nun,$(this));
                    updateCookie(nun,gid,$(this));
                })
                $(document).on("blur","[name=count]",function () {
                    let nun=Number($(this).parent().find("[name=count]").val());
                    if(/\D+/.test(nun)||nun<1){
                        nun=1;
                        $(this).val(1);
                    }
                    $(this).parent().find("[name=count]").val(nun);
                    //updateCount(oUser.u_id,gid,nun,$(this));
                })
            }

        }else {
            //如果登录，就从数据库中拿数据
            let oUser=JSON.parse(sessionStorage.getItem("userinfo"));
            $.ajax({
                url:"../../server/getCartByUser.php",
                data:{u_id:oUser.u_id},
                type:"post"
            }).then(function (res) {
                let str=``;
                let total=0;
                let number1=0;
                let number2=0;
                let arr=[];
                res.forEach((ele,index)=>{
                    str+=`<tr >
						<td><input type="checkbox" checked></td>
						<td><img src="${ele.c_img}" alt=""><span>${ele.c_name}</span></td>
						<td>${ele.g_id}</td>
						<td>¥<span>${ele.c_price}</span></td>
						<td>有库存</td>
						<td>是</td>
						<td data-id="${ele.g_id}"><a  href="javascript:;;" class="down">-</a><input type="text" value="${ele.c_num}"  name="count"><a href="javascript:;;" class="up">+</a></td>
						<td>¥ <span>${ele.c_price*ele.c_num}</span>.00</td>
						<td><a href="">收藏</a><a href="javascript:;" class="delete">删除</a></td>
					  </tr>`
                    total+=ele.c_price*ele.c_num;
                    number2+=Number(ele.c_num);
                    arr.push(str);
                })
                $(".statistical table tr td:last-child span").html(total);
                $("#num4").html(total);
                $("#num1").html(arr.length);
                $("#num2").html(number2);
                $(".shoppingCar_tab_bottom table tbody").html(str);
                del();
                let gid=$(".up").parent().data("id");
                //console.log(gid);
                $(document).on("click",".up",function () {
                    let nun=Number($(this).parent().find("[name=count]").val())+1;
                    $(this).parent().find("[name=count]").val(nun);
                    updateCount(oUser.u_id,gid,nun,$(this));
                })
                $(document).on("click",".down",function () {
                    let nun=Number($(this).parent().find("[name=count]").val())-1;
                    if (nun<1){
                        nun=1;
                    }
                    $(this).parent().find("[name=count]").val(nun);
                    updateCount(oUser.u_id,gid,nun,$(this));
                })
                $(document).on("blur","[name=count]",function () {
                    let nun=Number($(this).parent().find("[name=count]").val());
                    if(/\D+/.test(nun)||nun<1){
                        nun=1;
                        $(this).val(1);
                    }
                    $(this).parent().find("[name=count]").val(nun);
                    updateCount(oUser.u_id,gid,nun,$(this));
                })
            })
        }
    })
function updateCount(uid,gid,num,$self) {
    $.ajax({
        url:"http://10.41.151.57/fstvgo/server/updateCount.php",
        data:{
            uid:uid,
            gid:gid,
            num:num
        },
    }).then(function (res) {
        let price=Number($self.parent().siblings().eq(3).find("span").html());
        $self.parent().next().html("￥"+num*price+".00");
    })

}
function updateCookie(num,gid,$this) {
    let cookie=JSON.parse($.cookie("carts")||'[]');
    cookie.forEach((ele,index)=>{
        if(ele.gid==gid){
            ele.num=num;
        }
    })
    $.cookie("carts",JSON.stringify(cookie));
    let newCookie=JSON.parse($.cookie("carts")||'[]');
    let total=0;
    newCookie.forEach((ele,index)=>{
        if(ele.gid==gid){
            total+=ele.price*ele.num;
        }
    })
    console.log($(this).parent().next());
    $this.parent().next().html("￥"+total+".00");

}

})()